import Taro, { Component } from '@tarojs/taro'
import { View, Text } from '@tarojs/components'
import { AtIcon } from 'taro-ui'
import './myOrders.scss'
import app from '../../main.js'

export default class MyOrders extends Component {

    config = {
        navigationBarTitleText: '我的订单'
    }
    constructor() {
        super(...arguments)
        this.state = {
            orderArray: [
            ],
        }
    }
    gotoOrderss(orderId){
        app.navigateTo('../myOderss/myOderss'+'?id='+orderId+'&orderss=订单')
        console.log(22)
    }

    offset = 0;
    length = 25;
    total = 0;
    isFinlsh = false;

    //上拉加载
    onReachBottom() {
        if ((this.offset + this.length) < this.total) {
            app.toast('正在加载----')
            this.offset = this.offset + this.length;
            this.isFinlsh = false;
            //调用接口
            this.getDate();
        } else {
            app.toast('没有更多数据了咯')
            this.isFinlsh = true;
        }
    }
    // 获取数据
    getDate(){
        let me = this
        let data = {offset:this.offset,length:this.length}
        let dataArray =[]
        app.post('web/myself/myOrderList',data,function(res){
            console.log(123,res);
            dataArray = me.state.orderArray.concat(res.data.result.list)
            me.total = res.data.result.total
            me.setState({
                orderArray:dataArray
            },()=>{console.log(558,me.state.orderArray)})
           
        })
    }
    
    //重新获取数据
    getnNewDate(){
        let me = this
        let data = {offset:0,length:(this.offset + this.length)}
        let dataArray =[]
        app.post('web/myself/myOrderList',data,function(res){
            console.log(123,res);
            dataArray =res.data.result.list
            me.total = res.data.result.total
            me.setState({
                orderArray:dataArray
            })
        })
    }

    index = 0
    componentDidMount(){
        this.getDate()
        this.index += 1 ; 
    }

    componentDidShow () {
        console.log("执行didshow",this.index)
        if(this.index > 1 ){
            this.getnNewDate();
        }
    }

    render() {
        console.log(this.state.orderArray)
        return (
            <View className='bigBox'>
                <View className='topTitle'>
                    <Text className='title'>全部订单</Text>
                    <AtIcon value='chevron-down' size='20' />
                </View>
                {
                    this.state.orderArray.map((item) => {
                        return  <View className='content' onClick={this.gotoOrderss.bind(this,item.id)} key={item.id}>
                                    <View className='contentHead'>
                                        <View className='title'>{item.type}</View>
                                        <View className={item.status == '待支付' || item.status == '咨询中' || item.status == '待回复' ?'state-sel':'state-nosel'}>{item.status}</View>
                                    </View>
                                    <View className='contentDoc'>
                                        <Text>医生：</Text>
                                        <Text className={item.type == '快速咨询' ? ((item.status == '待支付' || item.status == '已支付' || item.status == '待接单') ? 'yishengmingzinone' : 'yishengmingzi') : ''}>{item.doctor_name}</Text>
                                    </View>
                                    <View className='contentPri'>
                                        <Text>实付金额：</Text>
                                        <Text>￥{item.totalpayble}</Text>
                                    </View>
                                    <View className='contentDate'>
                                        <Text>{item.created_at}</Text>
                                    </View>
                                </View>
                    })
                }
            </View>
        )
    }
}